#!/usr/bin/python
# -*- coding: utf-8 -*-

#This file is part of pyLot library.
#
# pyLot is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# pyLot is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with pyLot.  If not, see <http://www.gnu.org/licenses/>.

__author__ = u'Pierre Puiseux, Guillaume Baty'
__copyright__ = u"Copyright 2011-2012 (C) andheo, Université de Pau et des Pays de l'Adour"
__credits__ = [u'Pierre Puiseux', u'Guillaume Baty']
__license__ = "GNU Lesser General Public License"

__all__ = [u'WidgetSelectorInterface']

from Qt4 import QtGui
from _pylot.qtgui.interfaces.basewidgetinterface import BaseWidgetInterface
from _pylot.qtgui.styles.palettes import palette_fail, palette_ok

class WidgetSelectorInterface(BaseWidgetInterface):
  u"""
  An interface for value selector widgets.
  A selector is a widget handling simple data like int, float, Path or
  eventually a little bit more complex objects like list or dict.
  A data is considered "simple" if exec(repr(data)) == data which is the
  case for all base types.
  
  To handle more complex data, see WidgetEditorInterface.
  
  To implement this interface :
    - you **must** implement : _setValue, _getValue, valueChanged
    - you **should** implement : _checkValue
    
  """
  def __init__(self):
    pass

  def _setValue(self, value):
    """
    Just put value in widget, do not check here !
    """
    raise NotImplementedError

  def _value(self):
    raise NotImplementedError

  def _checkValue(self, value):
    u"""
    Must return one of these values :
    
      * True  : value is valid
      * False : value is not valid
      * None (default): you don't want to check value
    """
    return None

  def _dataWidget(self):
    raise NotImplementedError

  def setValue(self, value):
    u"""
    Set value to selector.
    For example, the setValue method for QLineEdit is setText.
    setValue() will emit valueChanged() if the new value is different 
    from the old one.
    """
    self._setValue(value)

  def value(self):
    u"""
    Return current value
    """
    return self._value()

  def _emitValueChanged(self):
    raise NotImplementedError

  def dataWidget(self):
    u"""
    Return widget containing data (value)
    """
    widget = self._dataWidget()
    if isinstance(widget, QtGui.QWidget) :
      return widget
    else :
      raise TypeError, "_dataWidget() must return a QWidget or derivated"

  def emitValueChanged(self):
    self._emitValueChanged()

  def checkValue(self):
    test = self._checkValue(self.value())
    if test is False :
      self.dataWidget().setPalette(palette_fail)
    elif test is True :
      self.dataWidget().setPalette(palette_ok)
    else :
      return
